package com.stars.easyms.logger.config;

import com.stars.easyms.base.util.ApolloListenUtil;
import com.stars.easyms.base.util.ClassExistsUtil;
import com.stars.easyms.logger.enums.LogFrameworkType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * <p>className: EasyMsLoggerHelper</p>
 * <p>description: EasyMs日志工具类</p>
 *
 * @author guoguifang
 * @date 2019-11-19 13:32
 * @since 1.4.0
 */
@Slf4j
public final class EasyMsLoggerHelper {

    private static final EasyMsLoggerProperties EASY_MS_LOGGER_PROPERTIES = EasyMsLoggerProperties.getInstance();

    private static LogFrameworkType logFrameworkType;

    private static String exceptionDesc;

    private static boolean redefineSuccess;

    private static boolean initSuccess;

    static synchronized void initLoggerProperties(ConfigurableApplicationContext applicationContext, boolean redefineSuccess) {
        EasyMsLoggerHelper.redefineSuccess = redefineSuccess;

        // 初始化属性
        initLoggerProperties(applicationContext.getEnvironment());

        // 创建apollo动态修改
        if (ClassExistsUtil.isExistApollo()) {
            ApolloListenUtil.listenWithChangedKeys(applicationContext, changedKeys -> {
                if (changedKeys.stream()
                        .anyMatch(key -> EasyMsLoggerProperties.LOG_ASYN_ENABLED_CONFIG.equals(key)
                                || EasyMsLoggerProperties.LOG_SIFT_ASYN_ENABLED_CONFIG.equals(key))) {
                    initLoggerProperties(applicationContext.getEnvironment());
                }
            });
        }

        // 初始化成功
        initSuccess = true;
    }

    private static synchronized void initLoggerProperties(ConfigurableEnvironment environment) {
        boolean oldIsSiftAsynEnabled = isSiftAsynEnabled();
        String logAsynEnabledStr = environment.getProperty(EasyMsLoggerProperties.LOG_ASYN_ENABLED_CONFIG);
        boolean localLogAsynEnabled = StringUtils.isBlank(logAsynEnabledStr) || "true".equalsIgnoreCase(logAsynEnabledStr);
        if (localLogAsynEnabled != EASY_MS_LOGGER_PROPERTIES.logAsynEnabled) {
            EASY_MS_LOGGER_PROPERTIES.logAsynEnabled = localLogAsynEnabled;
            log.info("{} easy-ms asynchronous logging!", isLogAsynEnabled() ? "Enable" : "Disable");
        }

        String siftAsynEnabledStr = environment.getProperty(EasyMsLoggerProperties.LOG_SIFT_ASYN_ENABLED_CONFIG);
        boolean localSiftAsynEnabled = StringUtils.isBlank(siftAsynEnabledStr) || "true".equalsIgnoreCase(siftAsynEnabledStr);
        if (localSiftAsynEnabled != EASY_MS_LOGGER_PROPERTIES.siftAsynEnabled) {
            EASY_MS_LOGGER_PROPERTIES.siftAsynEnabled = localSiftAsynEnabled;
            if (isSiftAsynEnabled() != oldIsSiftAsynEnabled) {
                log.info("{} easy-ms sift asynchronous logging!", isSiftAsynEnabled() ? "Enable" : "Disable");
            }
        }
    }

    public static boolean isRedefineSuccess() {
        return redefineSuccess;
    }

    public static boolean isLogAsynEnabled() {
        return isRedefineSuccess() && EASY_MS_LOGGER_PROPERTIES.logAsynEnabled;
    }

    public static boolean isSiftAsynEnabled() {
        return isLogAsynEnabled() && EASY_MS_LOGGER_PROPERTIES.siftAsynEnabled;
    }

    public static boolean isInitSuccess() {
        return initSuccess;
    }

    public static LogFrameworkType getLogFrameworkType() {
        return logFrameworkType;
    }

    public static String getExceptionDesc() {
        return exceptionDesc;
    }

    static void setLogFrameworkType(LogFrameworkType logFrameworkType) {
        EasyMsLoggerHelper.logFrameworkType = logFrameworkType;
    }

    static void setExceptionDesc(String exceptionDesc) {
        EasyMsLoggerHelper.exceptionDesc = exceptionDesc;
    }

    private EasyMsLoggerHelper() {
    }
}